//
//  LoginMainViewController.swift
//  YUANSHI
//
//  Created by pro on 2019/12/31.
//  Copyright © 2019 wangbin. All rights reserved.
//

import UIKit
import Alamofire
class LoginMainViewController: WBBaseViewController {

    let accoutTextField = UnderlineTextField();
    
    let passwordTextField = UnderlineTextField();
    
    let loginButton = UIButton();
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI();
    }
    
    func setUI()  {
        self.navigationController?.navigationBar.isHidden = true;
        
        let backButton = UIButton.init(type: .custom);
        backButton.setImage(UIImage(named: "bx_klineShare_close")?.withRenderingMode(.alwaysOriginal), for: .normal);
        backButton.addTarget(self, action: #selector(backButtonEven), for: .touchUpInside);
        self.view.addSubview(backButton);
        backButton.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20);
            make.top.equalToSuperview().offset(30);
        }
        
        let resegaseButton = UIButton.init(type: .custom);
        resegaseButton.setTitle("注册", for: .normal);
        resegaseButton.setTitleColor(UIColor.orange, for: .normal);
        resegaseButton.titleLabel?.font = UIFont.systemFont(ofSize: 13);
        self.view .addSubview(resegaseButton);
        resegaseButton.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-20);
            make.centerY.equalTo(backButton);
        }
        
        let attStr = NSMutableAttributedString.init(string: "登录您的\nBit Show账号");
        attStr.addAttributes([NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 20)], range: NSMakeRange(4, 9))
        let contLabel = UILabel.init();
        contLabel.font = UIFont.systemFont(ofSize: 20);
        contLabel.textColor = UIColor.black;
        contLabel.attributedText = attStr;
        contLabel.numberOfLines = 0;
        self.view.addSubview(contLabel);
        contLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(20);
            make.top.equalTo(backButton.snp_bottom).offset(40);
        }
        
        accoutTextField.placeholder = "请输入您的账号";
        accoutTextField.textColor = UIColor.lightGray;
//        accoutTextField.backgroundColor = UIColor.red;
        self.view.addSubview(accoutTextField);
        accoutTextField.snp.makeConstraints { (make) in
            make.top.equalTo(contLabel.snp_bottom).offset(40);
            make.left.equalToSuperview().offset(20);
            make.right.equalToSuperview().offset(-20);
            make.height.equalTo(50);
        };
//
        passwordTextField.placeholder = "请输入您的密码";
        passwordTextField.textColor = UIColor.lightGray;
        self.view.addSubview(passwordTextField);
        passwordTextField.snp.makeConstraints { (make) in
            make.top.equalTo(accoutTextField.snp_bottom).offset(40);
            make.left.equalToSuperview().offset(20);
            make.right.equalToSuperview().offset(-20);
            make.height.equalTo(50);
        };
//
        loginButton.setTitle("登录", for: .normal);
        loginButton.backgroundColor = UIColor.orange;
        loginButton.layer.masksToBounds = true;
        loginButton.layer.cornerRadius = 5;
        loginButton.addTarget(self, action: #selector(loginButtonEven), for: .touchUpInside);
        self.view.addSubview(loginButton);
        loginButton.snp.makeConstraints { (make) in
            make.top.equalTo(passwordTextField.snp_bottom).offset(40);
            make.left.equalToSuperview().offset(20);
            make.right.equalToSuperview().offset(-20);
            make.height.equalTo(50);
        };
    }
    
    @objc func backButtonEven(){
        
        self.navigationController?.popViewController(animated: true);
    }
    
    @objc func loginButtonEven(){
        
        if (accoutTextField.text!.isEmpty || passwordTextField.text!.isEmpty) {
            let alertController = UIAlertController(title: nil, message: "账号或者密码不能为空!", preferredStyle: .alert);
            let sureBtn = UIAlertAction(title: "取消", style: .cancel) { (UIAlertAction) in
                
            }
            let cancnBtn = UIAlertAction(title: "确定", style: .default) { (UIAlertAction) in
                
            }
            
            alertController.addAction(sureBtn)
            alertController.addAction(cancnBtn)
            self.present(alertController, animated: true) {
                
            };
            
        }else{
            
            //开始登录
            self.loginButton(userName: accoutTextField.text!, passWord: passwordTextField.text!);
        }
        
        
    }
    
    func loginButton(userName : String , passWord : String)  {
        
        loginNetWork.request(target: loginNetApi.LoginUrlMethod(phone: userName, password: passWord), success: { (data) in
            print(data);
            
        }, error: { (errorcode) in
            
        }) { (MoyaError) in
            print(MoyaError);
        }
        
    }

}
